<?php
/**
 * Created by yaoyu.
 * User: Administrator
 * Date: 2020/4/3
 * Time: 10:36
 */

namespace App\Admin\Controllers;


use App\Model\Pictures;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Routing\Controller;

class PicturesController extends Controller
{
    use HasResourceActions;

    /**
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content->header('图片列表')
            ->body($this->grid());
    }

    /**
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content->header('创建')->body($this->form());
    }

    /**
     * @param $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑')
            ->description('描述')
            ->body($this->form()->edit($id));
    }

    /**
     * @return Form
     */
    public function form()
    {
        $form = new Form(new Pictures());

        $form->text('title','标题');
        $form->number('sort','排序');
        $form->text('module','模块');
        $form->file('imgurl', '图片');
        $form->display('id','ID');

        return $form;
    }

    /**
     * 数据
     * @return Grid
     */
    public function grid()
    {
        $grid = new Grid(new Pictures());

        $grid->id()->sortable();
        $grid->title('标题');
        $grid->module('模块');
        $grid->sort('排序');
        $grid->created_at('创建时间');
        $grid->updated_at('修改时间');
        return $grid;
    }

    /**
     * @param $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content->header('详情')
            ->body($this->detail($id));
    }

    /**
     * @param $id
     * @return Show
     */
    public function detail($id)
    {
        $show = new Show(Pictures::findOrFail($id));

        $show->id('ID');
        $show->title('标题');
        $show->module('模块');
        $show->imgurl('图片')->setEscape(false)->as(function($img_url){
            $path = env('APP_URL');
            $data='';
            return $data . "<img src='{$path}/upload/{$img_url}' width='140'/>";
        });
        $show->sort('排序');
        $show->created_at('创建时间');
        $show->updated_at('修改时间');

        return $show;
    }
}